package balmysundaycandy.more.low.level.xmpp;

import com.google.appengine.api.xmpp.XMPPServicePb.PresenceRequest;
import com.google.appengine.api.xmpp.XMPPServicePb.PresenceResponse;
import com.google.appengine.api.xmpp.XMPPServicePb.XmppInviteRequest;
import com.google.appengine.api.xmpp.XMPPServicePb.XmppInviteResponse;
import com.google.appengine.api.xmpp.XMPPServicePb.XmppMessageRequest;
import com.google.appengine.api.xmpp.XMPPServicePb.XmppMessageResponse;
import com.google.appengine.repackaged.com.google.io.protocol.ProtocolMessage;
import com.google.apphosting.api.ApiProxy;

/**
 * xmpp operation.
 * 
 * @author marblejenka
 *
 * @param <Request>
 * @param <Response>
 */
abstract class XmppOperation<Request extends ProtocolMessage<?>, Response extends ProtocolMessage<?>> {
	private static final String packageName = "xmpp";

	public abstract Response call(Request request);

	public static final class GetPresenceOperation extends XmppOperation<PresenceRequest, PresenceResponse> {
		@Override
		public PresenceResponse call(PresenceRequest request) {
			PresenceResponse response = new PresenceResponse();
			response.mergeFrom(ApiProxy.makeSyncCall(packageName, "GetPresence", request.toByteArray()));
			return response;
		}
	}

	public static final class SendInviteOperation extends XmppOperation<XmppInviteRequest, XmppInviteResponse> {
		@Override
		public XmppInviteResponse call(XmppInviteRequest request) {
			XmppInviteResponse response = new XmppInviteResponse();
			response.mergeFrom(ApiProxy.makeSyncCall(packageName, "SendInvite", request.toByteArray()));
			return response;
		}
	}
	
	public static final class SendMessageOperation extends XmppOperation<XmppMessageRequest, XmppMessageResponse> {
		@Override
		public XmppMessageResponse call(XmppMessageRequest request) {
			XmppMessageResponse response = new XmppMessageResponse();
			response.mergeFrom(ApiProxy.makeSyncCall(packageName, "SendMessage", request.toByteArray()));
			return response;
		}
	}
}
